/**
 *******************************************************************************
 * @file can_driver.h
 * @author .ISS_AUTO (hudandan@issauto.com)
 * @date 2025-05-10
 *
 * @brief Project: AK601
 *
 * @copyright Copyright (c) 2025 ISSAUTO TECH Co., Ltd. All rights reserved.
 *
 *******************************************************************************
 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CAN_DRIVER_H_
#define __CAN_DRIVER_H_

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Exported types ------------------------------------------------------------*/
typedef struct __CAN_DriverTypeDef {
    CAN_Module    *Instance;           /*!< CAN registers base address           */

    uint32_t      FilterID[14];        /*!< Specifies the filter identification number (MSBs for a 32-bit
                                            configuration.
                                            This parameter can be a value between 0x0000 and 0xFFFF           */

    uint32_t      FilterID_Num;        /*!< Specifies the filter which will be initialized. It ranges from 0 to 13. */

    void (*IrqHandlerISR)(struct __CAN_DriverTypeDef *dcan); /*!< ADC function pointer on IrqHandler   */

    CanTxMessage  TxMessage;           /*!< TX message parameters                */

    CanRxMessage  RxMessage;           /*!< RX message parameters                */

} CAN_DriverTypeDef;

/* Exported constants --------------------------------------------------------*/

#define  CAN_MODE_TYPE_NORMAL                (1u)
#define  CAN_MODE_TYPE_LOOPBACK              (2u)
#define  CAN_MODE_TYPE_SILENT                (3u)
#define  CAN_MODE_TYPE_SILENT_LOOPBACK       (4u)

#define  CAN_MODE_TYPE                       (1u) // CAN_MODE_TYPE_NORMAL

#if (CAN_MODE_TYPE == CAN_MODE_TYPE_NORMAL)
    #define  CAN_OPERATING_MODE                   CAN_Normal_Mode
#elif (CAN_MODE_TYPE == CAN_MODE_TYPE_LOOPBACK)
    #define  CAN_OPERATING_MODE                   CAN_LoopBack_Mode
#elif (CAN_MODE_TYPE == CAN_MODE_TYPE_SILENT)
    #define  CAN_OPERATING_MODE                   CAN_Silent_Mode
#elif (CAN_MODE_TYPE == CAN_MODE_TYPE_SILENT_LOOPBACK)
    #define  CAN_OPERATING_MODE                   CAN_Silent_LoopBack_Mode
#endif


#define  CAN_BAUDRATE_1M                     (1u)
#define  CAN_BAUDRATE_500K                   (2u)
#define  CAN_BAUDRATE_250K                   (3u)
#define  CAN_BAUDRATE_125K                   (4u)

#define  CAN_BAUDRATE                        (2u) // CAN_BAUDRATE_500K

#if (CAN_BAUDRATE == CAN_BAUDRATE_1M)
    #define   CAN_BIT_RSJW                   CAN_RSJW_1tq
    #define   CAN_BIT_BS1                    CAN_TBS1_5tq
    #define   CAN_BIT_BS2                    CAN_TBS2_3tq
    #define   CAN_BAUDRATE_PRESCALER          4u
#elif (CAN_BAUDRATE == CAN_BAUDRATE_500K)
    #define   CAN_BIT_RSJW                   CAN_RSJW_1tq
    #define   CAN_BIT_BS1                    CAN_TBS1_14tq
    #define   CAN_BIT_BS2                    CAN_TBS2_3tq
    #define   CAN_BAUDRATE_PRESCALER          4u
#elif (CAN_BAUDRATE == CAN_BAUDRATE_250K)
    #define   CAN_BIT_RSJW                   CAN_RSJW_1tq
    #define   CAN_BIT_BS1                    CAN_TBS1_14tq
    #define   CAN_BIT_BS2                    CAN_TBS2_3tq
    #define   CAN_BAUDRATE_PRESCALER          8u
#elif (CAN_BAUDRATE == CAN_BAUDRATE_125K)
    #define   CAN_BIT_RSJW                   CAN_RSJW_2tq
    #define   CAN_BIT_BS1                    CAN_TBS1_13tq
    #define   CAN_BIT_BS2                    CAN_TBS2_4tq
    #define   CAN_BAUDRATE_PRESCALER          16u
#endif

/* Exported macro ------------------------------------------------------------*/
extern CAN_DriverTypeDef can1_driver;

/* Exported variables --------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
uint8_t CAN1_Tx_Message(uint32_t id, uint8_t *msg, uint8_t dlc);
CanRxMessage CAN1_Rx_Message(void);
void CAN1_LP_Configuration(void);
void CAN1_Configuration(struct __CAN_DriverTypeDef *dcan);

#endif

